package com.atguigu.sort;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

public class ShellSort {

	public static void main(String[] args) {
		int[] arr = { 8, 9, 1, 7, 2, 3, 5, 4, 6, 0 };
		
		System.out.println(Arrays.toString(arr));
		Date data1 = new Date();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date1Str = simpleDateFormat.format(data1);
		System.out.println("开始时间=" + date1Str);
		
		//shellSort(arr); 
		shellSort2(arr);
		
		Date data2 = new Date();
		String date2Str = simpleDateFormat.format(data2);
		System.out.println("结束时间=" + date2Str);
		
	
	}


	public static void shellSort(int[] arr) {
		
		int temp = 0;
		int count = 0;
	
		for (int gap = arr.length / 2; gap > 0; gap /= 2) {
			for (int i = gap; i < arr.length; i++) {
				
				for (int j = i - gap; j >= 0; j -= gap) {
				
					if (arr[j] > arr[j + gap]) {
						temp = arr[j];
						arr[j] = arr[j + gap];
						arr[j + gap] = temp;
					}
				}
			}
			
		}
		
		
	}
	

	public static void shellSort2(int[] arr) {
		
	
		for (int gap = arr.length / 2; gap > 0; gap /= 2) {
			
			for (int i = gap; i < arr.length; i++) {
				int j = i;
				int temp = arr[j];
				if (arr[j] < arr[j - gap]) {
					while (j - gap >= 0 && temp < arr[j - gap]) {
					
						arr[j] = arr[j-gap];
						j -= gap;
					}
					
					arr[j] = temp;
				}

			}

			System.out.println(gap);
			System.out.println(Arrays.toString(arr));
		}
	}

}
